00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef _line_wrapping_output_filter_hpp_
00028 #define _line_wrapping_output_filter_hpp_
00029
00030 #include <boost/iostreams/filtering_stream.hpp>
00031
00032 namespace io = boost::iostreams;
00033
00034 #include <boost/iostreams/filtering_stream.hpp>
00035
00036
00037
00038
00039
00040
00041 class line_wrapping_output_filter : public io::output_filter {
00042 public:
00043 explicit line_wrapping_output_filter(int line_length = 80, int margin = 8)
00044 : do_new_line_(false), line_length_(line_length), end_margin_(margin), col_no_(0)
00045 { }
00046
00047 template<typename Sink>
00048 bool put(Sink& dest, int c)
00049 {
00050
00051 if (do_new_line_) {
00052 if (c == '\n') {
00053 do_new_line_ = false;
00054 } else if (!std::isspace(c)) {
00055 if (!put_char(dest, '\n')) return false;
00056 do_new_line_ = false;
00057 } else {
00058
00059 return true;
00060 }
00061 } else if (col_no_ >= (line_length_ - end_margin_)) {
00062
00063 if (std::isspace(c)) {
00064 do_new_line_ = true;
00065 }
00066 }
00067 return put_char(dest, c);
00068 }
00069
00070 template<typename Sink>
00071 void close(Sink&)
00072 { col_no_ = 0; }
00073
00074 private:
00075 template<typename Sink>
00076 bool put_char(Sink& dest, int c)
00077 {
00078 if (!io::put(dest, c))
00079 return false;
00080 if (c != '\n')
00081 ++col_no_;
00082 else
00083 col_no_ = 0;
00084 return true;
00085 }
00086 int do_new_line_;
00087 int line_length_;
00088 int end_margin_;
00089 int col_no_;
00090 };
00091
00092 #endif